package random.fox.p2p.interactors;

import android.content.SharedPreferences;
import android.text.TextUtils;
import android.util.Log;

import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONObject;

import java.lang.reflect.Type;
import java.util.List;

import javax.inject.Inject;

import random.fox.p2p.App;
import random.fox.p2p.api.ApiClient;
import random.fox.p2p.bean.BeanModule;
import random.fox.p2p.bean.greendao.Tag;
import random.fox.p2p.bean.greendao.User;

/**
 * Created by wq on 15/3/16.
 */
public class LoginInteractorImpl implements LoginInteractor {
    private String TAG = getClass().getSimpleName();


    private App app;
    @Inject User user;
    public LoginInteractorImpl(App app) {
        this.app = app;
        app.createScopedGraph(new BeanModule()).inject(this);
    }

    @Override
    public String[] init() {
        SharedPreferences preferences = app.getSharedPreferences("login_info", 0);
        if (!preferences.getAll().isEmpty()) {
            String[] arr = new String[2];
            arr[0] = preferences.getString("name", "");
            arr[1] = preferences.getString("password", "");
            return arr;
        }
        return null;
    }

    @Override
    public void savePassword(String name, String password) {
        SharedPreferences preferences = app.getSharedPreferences("login_info", 0);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString("name", name);
        editor.putString("password", password);
        editor.commit();
    }

    @Override
    public void clearPassword() {
        SharedPreferences preferences = app.getSharedPreferences("login_info", 0);
        SharedPreferences.Editor editor = preferences.edit();
        editor.clear();
        editor.commit();
    }

    @Override
    public void login(final String username, final String password, final OnLoginFinishedListener listener) {
        ApiClient apiClient = app.getApiClient();
        try {
            apiClient.login(username, password, new ApiClient.CallBack<JSONObject>() {
                @Override
                public void onResponse(JSONObject jsonObject) {
                    String success = jsonObject.optString("success");
                    if(!TextUtils.isEmpty(success) || success.equals("1")){
                        listener.onPasswordError();
                        return;
                    }
                    Gson gson = new Gson();
                    Type type = new TypeToken<List<Tag>>() {}.getType();
                    List<Tag> tagList = gson.fromJson(jsonObject.optJSONArray("tags").toString(),type);
                    user.setTagList(tagList);

                    String uid = jsonObject.optString("uid");
                    String nickname = jsonObject.optString("nickname");
                    int sex = jsonObject.optInt("sex");
                    String headimg = jsonObject.optString("headimg");
                    user.setUid(uid);
                    user.setNickname(nickname);
                    user.setSex(sex);
                    user.setHeadimg(headimg);

                    if(!jsonObject.isNull("birthday")){
                        String birthday = jsonObject.optString("birthday");
                        user.setBirthday(birthday);
                    }
                    if(!jsonObject.isNull("city")){
                        String city = jsonObject.optString("city");
                        user.setCity(city);
                    }
                    if(!jsonObject.isNull("school")){
                        String school = jsonObject.optString("school");
                        user.setSchool(school);
                    }
                    listener.onSuccess(username, password);

                }

                @Override
                public void onErrorResponse(VolleyError volleyError) {
                    Log.e(TAG, volleyError.toString());
                    listener.onPasswordError();
                }
            });
        } catch (Exception e) {
            Log.e(TAG, e.toString());
            listener.onPasswordError();
        }

    }
}
